package test;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//1、创建一个Product类，包含以下属性(id,name,price,info(产品说明),num(销量))   2分
public class Product {

    private Integer id;
    private String name;
    private Integer price;
    private String info;
    private Integer num;

    public Product() {
    }

    public Product(Integer id, String name, Integer price, String info, Integer num) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.info = info;
        this.num = num;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", info='" + info + '\'' +
                ", num=" + num +
                '}';
    }
}

class Test{
    List<Product> productList = new ArrayList<>();
    public static void main(String[] args) {

        Test test = new Test();
        Product[] products = test.fun1();
        test.fun2(products);
        test.fun3();
        test.fun4();
        test.fun5();
        List<Product> list = test.fun6();
        test.fun7();

        test.fun8(list);

    }

    //将上面的随机得到的集合使用对象流存入到product.obj文件中   5分
    private void fun8(List<Product> list) {
        System.out.println("将上面的随机得到的集合使用对象流存入到product.obj文件中");
        list.stream().forEach(System.out::println);
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("demo24\\src\\test\\product.obj"))){
            for (int i = 0; i < list.size(); i++) {
                if (i == list.size() - 1){
                    bw.write(list.get(i).toString());
                }else {
                    bw.write(list.get(i).toString());
                    bw.newLine();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //判断当前工程目录下是否有product.obj文件，如果存在输出存在该文件如果不存在创建文件 5分
    private void fun7() {
        System.out.println("判断当前工程目录下是否有product.obj文件，如果存在输出存在该文件如果不存在创建文件");
        File file = new File("demo24\\src\\test\\product.obj");
        if (file.exists()) {
            System.out.println("存在该文件");
        } else {
            System.out.println("不存在该文件");
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //随机获取List集合中的5个对象,放到新的List集合中。5分
    private List<Product>  fun6() {
        System.out.println("随机获取List集合中的5个对象,放到新的List集合中。");
        //生成5个不同的随机数
        int[] nums = getNums();
        System.out.println(Arrays.toString(nums));
        productList.stream().filter(product -> {
            for (int num : nums) {
                if (product.getId() == num + 1) {
                    return true;
                }
            }
            return false;
        }).forEach(System.out::println);

        List<Product> collect = productList.stream().filter(product -> {
            for (int num : nums) {
                if (product.getId() == num + 1) {
                    return true;
                }
            }
            return false;
        }).toList();

        return collect;
    }

    private int[] getNums() {
        int[] nums = new int[5];
        for (int i = 0; i < nums.length; i++) {
            int num = (int) (Math.random() * productList.size());
            if (i == 0) {
                nums[i] = num;
            } else {
                for (int j = 0; j < i; j++) {
                    if (nums[j] == num) {
                        i--;
                        break;
                    } else {
                        nums[i] = num;
                    }
                }
            }
        }
        return nums;
    }

    //获取List集合中价格大于100元的商品           5分
    private void fun5() {
        System.out.println("获取List集合中价格大于100元的商品");
        productList.stream().filter(product -> product.getPrice() > 100).forEach(System.out::println);
    }

    //将List集合中的数据使用三种方式打印输出在控制台 5分
    private void fun4() {
        System.out.println("将List集合中的数据使用三种方式打印输出在控制台");
        for (int i = 0; i < productList.size(); i++) {
            System.out.println(productList.get(i));
        }
        System.out.println("------------------");
        for (Product product : productList) {
            System.out.println(product);
        }
        System.out.println("------------------");
        productList.forEach(System.out::println);
    }

    //将集合中的Product对象按照价格进行降序排序   5分
    private void fun3() {
        System.out.println("将集合中的Product对象按照价格进行降序排序");
        productList.stream().sorted((o1, o2) -> o2.getPrice() -  o1.getPrice()).forEach(System.out::println);

    }

    //3、将数组中8个Product对象放入到List集合中     3分
    private void fun2(Product[] products) {
        System.out.println("将数组中8个Product对象放入到List集合中");

        productList.addAll(Arrays.asList(products));
    }

    //    2、创建一个长度为8的Product数组，创建8个Product对象存入数组中(数据尽量多样化) 2分
    private Product[] fun1() {
        System.out.println("创建一个长度为8的Product数组，创建8个Product对象存入数组中");

        Product[] products = new Product[8];
        products[0] = new Product(1, "苹果", 10, "苹果是水果", 100);
        products[1] = new Product(2, "香蕉", 20, "香蕉是水果", 200);
        products[2] = new Product(3, "西瓜", 30, "西瓜是水果", 300);
        products[3] = new Product(4, "火龙果", 111, "火龙果是水果", 400);
        products[4] = new Product(5, "苹果", 50, "苹果是水果", 500);
        products[5] = new Product(6, "香蕉", 60, "香蕉是水果", 600);
        products[6] = new Product(7, "西瓜", 70, "西瓜是水果", 700);
        products[7] = new Product(8, "火龙果", 80, "火龙果是水果", 800);

        return products;
    }
}
